// a buffer for all child
var BULLET_BUFFER=[];
var Bullet = cc.Sprite.extend({
    _power : 0,
    _speed : 0,
    _life : true,
    _xSpdScale : 1,
    _ySpdScale : 1,
    ///////////////////////////////////////////////////////////
    ctor : function(filename,power,speed){
        this._super(filename);
        this._power = power;
        this._speed = speed;
    },
    onEnter:function () {
        this._super();
        BULLET_BUFFER.push( this );
    },
    onExit : function(){
        this._super();
    },
    ////////////////////////// settings ////////////////////////
    getPower : function(){
        return this._power;
    },
    setPower : function(power){
        this._power=power;
    },
    getSpeed : function(){
        return this._speed;
    },
    setSpeed : function(speed){
        this._speed=speed;
    },
    getLife : function(){
        return this._life;
    },
    setLife : function(life){
        this._life=life;
    },
    revive : function(){
        this.setLife(true);
        this.setVisible(true);
    },
    recover : function(){
        this.setLife(false);
        this.setVisible(false);
    },
    ////////////////////////// angle //////////////////////////
    angle : function( targetPos ) {
        var fenZi =  targetPos.y - this.bornPos.y;
        var fenMu =  targetPos.x - this.bornPos.x;
        var angle = Math.atan( fenZi/fenMu );
        return -angle*180*0.318310;
    },
    // you must set this position first
    setSpeedScale : function(targetPos,distance){
        var xScale = ( targetPos.x - this.getPositionX() )/distance;
        var yScale = ( this.getPositionY() - targetPos.y )/distance;
        this._xSpdScale =  xScale;
        this._ySpdScale =  yScale;
        this.runAction( cc.RepeatForever.create(
            cc.RotateBy.create( 1,-360 )
        ) );
    },
    ////////////////////////// moving //////////////////////////
    moving : function(dt){
        if ( this.isVisible() ){
            var x = this.x, y = this.y;
            var rota = this.getRotation();
            this.x = x + this._speed * this._xSpdScale * dt;
            this.y = y - this._speed * this._ySpdScale * dt;
            var winSize = cc.director.getWinSize();
            if ( x < 0 || x > winSize.width || y < 0 || y > winSize.height ) {
                this.recover();
                this.stopAllActions();
            }
        }
    }
});

